package com.example.xd.defaultcall;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telecom.TelecomManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {

    private static final int REQUEST_OVERLAY = 1;
    private EditText mEt_phoneNum;
    private TelecomManagerUitls mTelecomManagerUitls;
    String[] permissions = {
            Manifest.permission.CALL_PHONE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.MODIFY_PHONE_STATE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_call).setOnClickListener(this);
        findViewById(R.id.btn_set_default).setOnClickListener(this);
        findViewById(R.id.btn_get_floating_permission).setOnClickListener(this);
        findViewById(R.id.btn_get_phone_permission).setOnClickListener(this);
        findViewById(R.id.btn_test).setOnClickListener(this);
        mEt_phoneNum = findViewById(R.id.et_phoneNum);
        mTelecomManagerUitls = TelecomManagerUitls.getInstance(this);
    }

    public void requestRole() {
        if (!mTelecomManagerUitls.isDefaultCall()) {
            Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
            intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());
            startActivity(intent);
        } else {
            Toast.makeText(this, "已是默认通话应用", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (!Settings.canDrawOverlays(MainActivity.this)) {
            Toast.makeText(this, "获取悬浮窗权限成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "获取悬浮窗权限失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> list) {
        if (requestCode == 2) {
            Toast.makeText(this, "以获取电话相关权限成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
        if (requestCode == 2) {
            Toast.makeText(this, "获取电话相关权限失败", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_call:
                call();
                break;
            case R.id.btn_set_default:
                requestRole();
                break;
            case R.id.btn_get_floating_permission:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.canDrawOverlays(MainActivity.this)) {
                        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:" + getPackageName()));
                        startActivityForResult(intent, REQUEST_OVERLAY);
                    } else {
                        Toast.makeText(this, "有悬浮窗权限", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.btn_get_phone_permission:
                if (EasyPermissions.hasPermissions(this, permissions)) {
                    Toast.makeText(this, "以获取电话相关权限", Toast.LENGTH_SHORT).show();
                } else {
                    EasyPermissions.requestPermissions(this, "获取权限", 2, permissions);
                }
                break;
            case R.id.btn_test:
                startActivity(new Intent(MainActivity.this, EditActivity.class));
                break;
        }
    }

    private void call() {
        String phoneNum = mEt_phoneNum.getText().toString();
        TelecomManagerUitls instance = TelecomManagerUitls.getInstance(this);
        instance.call(phoneNum);
    }
}
